from abc import ABC, abstractmethod
from typing import Dict, Any, Optional
import httpx
from models.m import User

class SocialLoginProvider(ABC):
    """社交登录提供者的抽象基类"""
    
    def __init__(self, config: Dict[str, Any]):
        self.config = config
    
    @abstractmethod
    async def get_authorization_url(self) -> str:
        """获取授权URL"""
        pass
    
    @abstractmethod
    async def exchange_code_for_token(self, code: str) -> Dict[str, Any]:
        """用授权码换取访问令牌"""
        pass
    
    @abstractmethod
    async def get_user_info(self, access_token: str) -> Dict[str, Any]:
        """获取用户信息"""
        pass
    
    @abstractmethod
    async def save_user_info(self, user_info: Dict[str, Any], token_info: Dict[str, Any]) -> User:
        """保存用户信息到数据库"""
        pass


class SocialLoginFactory:
    """社交登录工厂类"""
    
    _providers = {}
    
    @classmethod
    def register_provider(cls, name: str, provider_class):
        """注册登录提供者"""
        cls._providers[name] = provider_class
    
    @classmethod
    def create_provider(cls, name: str, config: Dict[str, Any]) -> Optional[SocialLoginProvider]:
        """创建登录提供者实例"""
        provider_class = cls._providers.get(name)
        if provider_class:
            return provider_class(config)
        return None
    
    @classmethod
    def get_available_providers(cls) -> list:
        """获取所有可用的登录提供者"""
        return list(cls._providers.keys())